﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using OGS.Contracts;

namespace OGS.DataAccess
{
    /// <summary>
    /// Represents a class used for database related work of User
    /// </summary>
    public class DAUser
    {
        private static readonly DAUser instance;

        /// <summary>
        /// Initializes a static instance of the DAUser class with no arguments.
        /// </summary>
        static DAUser()
        {
            instance = new DAUser();
        }

        /// <summary>
        /// Initializes a new instance of the DAUser class with no arguments.
        /// </summary>
        private DAUser() { }

        /// <summary>
        /// The Instance of DAUser class
        /// </summary>
        public static DAUser Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Saves the User
        /// </summary>
        /// <param name="objBrandInfo"> The BrandInfo Object </param>
        /// <returns>Success/Faliure</returns>
        public int SaveUser(User oUserInfo)
        {
            using (DBManager dbm = new DBManager("stp_Save_User"))
            {
                dbm.Add("@UserID", oUserInfo.UserID, ParameterDirection.InputOutput);
                dbm.Add("@UserName", oUserInfo.UserName);
                dbm.Add("@Password", oUserInfo.Password);
                dbm.Add("@Name", oUserInfo.Name);
                dbm.Add("@Email", oUserInfo.Email);
                dbm.Add("@Phone", oUserInfo.Phone);
                dbm.Add("@ShopName", oUserInfo.ShopName);
                dbm.Add("@Address", oUserInfo.Address);
                dbm.Add("@Landmark", oUserInfo.Landmark);
                dbm.Add("@Zip", oUserInfo.Zip);
                dbm.Add("@Area", oUserInfo.Area);
                dbm.Add("@State", oUserInfo.State);
                dbm.Add("@RoleID", oUserInfo.Role.RoleID);
                dbm.Add("@IsBlocked", oUserInfo.IsBlocked);
                dbm.Add("@IsActive", oUserInfo.IsActive);
                dbm.Add("@Location", oUserInfo.Location);
                dbm.Add("@Latitude", oUserInfo.Latitude);
                dbm.Add("@Longitude", oUserInfo.Longitude);
                if (oUserInfo.LastModifiedBy > 0)
                {
                    dbm.Add("@LastModifiedBy", oUserInfo.LastModifiedBy);
                }
                dbm.Add("@LastModifiedDate", oUserInfo.LastModifiedDate);

                dbm.ExecuteNonQuery();

                oUserInfo.UserID = Convert.ToInt64(dbm.GetValueInInt("@UserID"));
                return Convert.ToInt32(dbm.ReturnValue);
            }
        }

        /// <summary>
        /// Deletes the Brand
        /// </summary>
        /// <param name="ID"> The BrandInfo ID </param>
        /// <returns>Success/Faliure</returns>
        public int DeleteBrand(int ID)
        {
            using (DBManager dbm = new DBManager("stp_Delete_Brand"))
            {
                dbm.Add("@BrandID", ID);

                dbm.ExecuteNonQuery();
                return Convert.ToInt32(dbm.ReturnValue);
            }
        }

        /// <summary>
        /// Get Brand Details
        /// </summary>
        /// <param name="ID"> The Brand ID </param>
        /// <returns>The Brand DataTable </returns>
        public User GetUserDetails(int ID)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Get_UserDetails"))
            {
                dbm.Add("@UserID", ID);

                dt = dbm.GetDataTable();
            }

            User objUser = new User();
            if (dt.Rows.Count > 0)
            {
                objUser = PopulateUser(dt.Rows[0]);
            }
            return objUser;
        }

        /// <summary>
        /// Search the Brand
        /// </summary>
        /// <param name="objBrandInfo"> The BrandInfo Object </param>
        /// <param name="objSearch"> The SearchAttributes Object </param>
        /// <returns>The Brand DataTable </returns>
        public List<User> SearchUser(User oUser, SearchAttributes objSearch)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Search_User", objSearch))
            {
                //dbm.Add("@Name", objBrandInfo.Name);
                //dbm.Add("@IsActive", objBrandInfo.IsActive);
                dt = dbm.GetDataTable();

                objSearch.TotalRecordCount = Convert.ToInt32(dbm.GetValueInInt("@TotalRecordCount"));
            }

            List<User> lstUser = new List<User>();
            foreach (DataRow dr in dt.Rows)
            {
                User objUser = PopulateUser(dr);
                lstUser.Add(objUser);
            }
            return lstUser;

        }

        /// <summary>
        /// Populate the Brand
        /// </summary>
        /// <param name="dr"> The BrandInfo DataRow </param>
        /// <returns>The Brand Object </returns>
        public User PopulateUser(DataRow dr)
        {
            User oUser = new User();

            oUser.UserID = Convert.ToInt32(dr["PK_UserID"]);
            oUser.Name = Convert.ToString(dr["Name"]);
            oUser.Email = Convert.ToString(dr["Email"]);
            oUser.ShopName = Convert.ToString(dr["ShopName"]);
            oUser.UserName = Convert.ToString(dr["Username"]);
            oUser.Address = Convert.ToString(dr["Address"]);
            oUser.Landmark = Convert.ToString(dr["Landmark"]);
            oUser.Phone = Convert.ToString(dr["PhoneNo"]);
            oUser.Zip = Convert.ToString(dr["Zip"]);
            oUser.Area = Convert.ToString(dr["Area"]);
            oUser.State = Convert.ToString(dr["State"]);
            oUser.Role = new Role() { RoleID = Convert.ToInt32(dr["RoleID"]) };
            oUser.IsBlocked = Convert.ToBoolean(dr["IsBlocked"]);
            oUser.IsActive = Convert.ToBoolean(dr["IsActive"]);
            oUser.Latitude = Convert.ToDecimal(dr["Latitude"]);
            oUser.Longitude = Convert.ToDecimal(dr["Longitude"]);
            oUser.Location = Convert.ToString(dr["Location"]);
            return oUser;
        }


    }
}
